<?php
class Users extends CI_Controller {
    
	public function index($success_message_3 = null){

			$this->load->model('Users_model');
			$alias = null;
			$name = null;
			$pass = null;
			$rol = null;
            if(isset($success_message_3)){
		$vars['success_message_3'] = true;
	   }		
			if($this->input->post('alias')){
            	$alias =$this->input->post('alias');
			}
			if($this->input->post('name')){
				$name = $this->input->post('name');
			}
			if($this->input->post('pass')){
				$pass = $this->input->post('pass');
			}
			if($this->input->post('rol')){
				$rol = $this->input->post('rol');
			}
						
             $users = $this->Users_model->getAll();
			if ($alias != null || $name != null || $pass != null || $rol != null) {
	       	 $users = $this->Users_model->search($alias, $name);
	    	}
			
			$vars['alias'] = $alias;
			$vars['name'] = $name;
			$vars['pass'] = $pass;
			$vars['rol'] = $rol;
			
				
            $vars['content_view'] =  'users_list';
			$vars['users'] = $users;
            $vars['menu'] = 'users';
            $this->load->view('template', $vars);

	}


	
	public function add($success_message = null){
		
	   if(isset($success_message)){
		$vars['success_message'] = true;
	   }
       
	   $this->load->model('Rols_model');
	   $vars['id_rol'] = $this->Rols_model->getAll();                       
       $vars['content_view'] = 'users_form'; // varibale cque contiene el nombre del formulario  llamar
	   $vars['menu'] = 'users'; //
	   $this->load->view('template', $vars);
	}
	
	public function edit($id)
     {
       $this->load->model('Users_model');
       $vars['user'] = $this->Users_model->find($id);
	   
       $this->load->model('Rols_model');
	   $vars['id_rol'] = $this->Rols_model->getAll(); 
	   
	   $vars['content_view'] = 'users_form'; // varibale cque contiene el nombre del formulario  llamar
	   $vars['menu'] = 'users'; //
	   $this->load->view('template', $vars);
                 
       /*********/
    }
	
	
	public function delete($id)
    {
        $this->load->model('Users_model');
        $this->Users_model->delete($id);
        $this->index();
    }
	
	
	public function submit(){
		
		if($this->input->post('user_id') != ''){
			
			$this->load->model('Users_model');
			
			
			
			$vars['alias'] = $this->input->post('alias');
	        $vars['name'] = $this->input->post('name');
	        $vars['password'] = $this->input->post('pass');
	        $vars['id_rol'] = $this->input->post('rol');
			
			$id = $this->input->post('user_id');
			$vars['edit'] = $this->Users_model->update($id, $vars);
			$vars['user'] = $this->Users_model->find($id);
			$this->load->model('Rols_model');
	        $vars['id_rol'] = $this->Rols_model->getAll(); 
            $vars['content_view'] = 'users_form'; // varibale cque contiene el nombre del formulario  llamar
            $vars['menu'] = 'users'; //
            $this->load->view('template', $vars);
			
			
		}else{
			
			$this->load->model('Users_model');
                
            $data= array();
               
	        $data['alias'] = $this->input->post('alias');
	        $data['name'] = $this->input->post('name');
	        $data['password'] = ($this->input->post('pass'));
	        $data['id_rol'] = $this->input->post('rol');
			$data['status'] = 1;
			
			$vars['success_message'] = $this->Users_model->add($data);
			$vars['content_view'] = 'users_form'; // varibale cque contiene el nombre del formulario  llamar
            $this->load->model('Rols_model');
	        $vars['id_rol'] = $this->Rols_model->getAll(); 
			$vars['menu'] = 'users'; //
            $this->load->view('template', $vars);
		}
	
	}
	
	
	public function detected_alias(){
	   $this->load->model('Users_model');
	   
	   $alias =$this->input->post('alias');
	   
       echo $this->Users_model->detected_alias($alias);	
	}
	
	public function edit_pass(){
		$this->load->model('Users_model');

		$id = $this->input->post('id');
		$vars['password'] = ($this->input->post('pass')); 
			
		$this->Users_model->update($id, $vars);
                $this->index(true);
		
	}


  

}
?>
